//
//  ViewController.m
//  7-11快医
//
//  Created by amor on 16/7/11.
//  Copyright © 2016年 amor. All rights reserved.
//

#import "CJTTableViewController.h"
#import "CJTModel.h"
#import "CJTTools.h"
#import "CJTDoctorMeansCell.h"
#import <YYModel.h>
#import "CJTDoctorDetailsController.h"

static NSString * const Identifier = @"a";

@interface CJTTableViewController ()
@property (strong, nonatomic) NSArray *data;

@end

@implementation CJTTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //    获取医生列表接口
    NSString *url = @"http://iosapi.itcast.cn/doctor/matchDoctors.json.php";
    NSDictionary *parameters = @{@"ci1_id":@1,@"ci2_id":@3,@"ci3_id":@3,@"diagnosis_type":@0,@"page_size":@15,@"is_confirmed":@1,@"user_id":@1000089, @"page":@1,@"has_diagnosis":@2};
    
    [CJTTools postURL:url parameter:parameters result:^(NSDictionary* responseObject, NSError *error) {
        if (error ==nil) {
            // 想要得到一个模型数组
            self.data = [NSArray yy_modelArrayWithClass:[Esarray class] json:responseObject[@"data"]];
            [self.tableView reloadData];
        } else {
            NSLog(@"%@",error);
        }
    }];
}


#pragma mark - tableView 代理

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJTDoctorMeansCell *cell = [CJTDoctorMeansCell doctorMeansCellWithTableView:tableView reuseIdentifier:Identifier indexPath:indexPath] ; //根据indexPath准确地取出一行，而不是从cell重用队列中取出
    
    //    doctorMeansCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier forIndexPath:indexPath];
    if (indexPath.row % 2 == 0) {
        cell.backgroundColor = [UIColor grayColor];
    }else{
        cell.backgroundColor = [UIColor whiteColor];
    }
    Esarray* arrayData = self.data[indexPath.row];
    cell.models = arrayData;
    return cell;
    
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

      UIStoryboard *sb = [UIStoryboard storyboardWithName:@"CJTDoctorStoryboard" bundle:nil];
    CJTDoctorDetailsController *vc = [sb instantiateInitialViewController];

    vc.arrayData = self.data[indexPath.row];
    
    [self.navigationController pushViewController:vc animated:YES];
}

-(NSArray *)data
{
    if (_data ==nil) {
        _data = [NSArray array];
    }
    return _data;
}
@end
